﻿using System.Windows;
using System.Windows.Controls;
using Application = System.Windows.Forms.Application;
using UserControl = System.Windows.Controls.UserControl;

namespace WPFGoogleMapControl
{
    /// <summary>
    /// Interaction logic for ChromBrowser.xaml
    /// </summary>
    public partial class MapControl : UserControl
    {
        #region DependencyProperties

        public new double Width
        {
            get { return (double)GetValue(WidthProperty); }
            set
            {
                SetValue(WidthProperty, value);
            }
        }

        public static readonly DependencyProperty WidthProperty =
            DependencyProperty.Register("Width", typeof(double), typeof(MapControl));

        public new double Height
        {
            get { return (double)GetValue(HeightProperty); }
            set
            {
                SetValue(HeightProperty, value);
            }
        }

        public static readonly DependencyProperty HeightProperty =
            DependencyProperty.Register("Height", typeof(double), typeof(MapControl));


        #endregion

        public MapControl()
        {
            InitializeComponent();
            browser.Focusable = true;
            browser.EnableAsyncRendering = false;
            Loaded += MapControl_Loaded;
        }

        void MapControl_Loaded(object sender, RoutedEventArgs e)
        {
            var url = string.Format("file://{0}/{1}", Application.StartupPath, "map.html");
            browser.Navigate(url);
        }
        
        #region Scripts Calls
        public void SetSource(LocationResult selectedLocation)
        {
            browser.ExecuteJavascript(string.Format("SetSource({0},{1})", selectedLocation.Latitude, selectedLocation.Longitude), "");
        }

        public void SetDestination(LocationResult selectedLocation)
        {
            browser.ExecuteJavascript(string.Format("SetDestination({0},{1})", selectedLocation.Latitude, selectedLocation.Longitude), "");
        }
        #endregion
    }
}
